<template>
  <list-layout :categories="categories"
    :originalCategory="Bus.$store.state.portal.filters.resource"
    @changed="categoryChanged" channel="dataset">
    <div class="list-top-text">
      {{$t((currentCategory && currentCategory.子库) || '全部资源')}}
      <filter-options ref="filterOptions" class="filter-options"
        :hasKw="true"
        :bottomKw="true"
        :opts="[...filterOptions, fieldsFilter]"
        :showSelected="false"
        :maxLines="3"
        @search="filter({query: $event})"></filter-options>
    </div>
    <list-item
      v-for="(item,index) in data.docs" :key="index"
      :tags="[item.类型, item.领域, item.场景, item.来源, item.格式]"
      :label="item.类型"
      :title="item.标题"
      :content="item.描述"
      :url="`/dataset/${item.id}`"></list-item>

    <div
      v-if="data && data.total === 0"
      class="no-data-label">
      <div class="absolute-center">
        {{$t('暂无数据')}}
      </div>
    </div>

    <!-- 列表页面注意分页 -->
    <!-- 翻页时调用: refreshData({page: <页吗>}) -->
    <pagination v-if="data && data.total" :data="data" @changed="pageChanged"></pagination>
  </list-layout>
</template>

<script>
import mixins from 'eis-admin-mixins';
import { RefreshOnLocaleChange } from '../../components/mixins/locale';
import listLayout from '../../components/listLayout.vue';
import listItem from '../../components/listItem.vue';
import pagination from '../../components/pagination.vue';
import FilterOptions from '../../components/filterOptions.vue';

export default {
  name: 'ResourceList',
  mixins: [mixins.ObjectDataMixin, RefreshOnLocaleChange],
  components: {
    listItem,
    listLayout,
    pagination,
    FilterOptions,
  },
  data() {
    return {
      filters: {},
      fieldsFilter: undefined,
      filterOptions: [
        {
          label: '类型',
          field: '类型',
          multiple: false,
          options: [
            {
              label: '模型',
              value: '模型',
            },
            {
              label: '数据集',
              value: '数据集',
            },
          ],
        },
      ],
      categories: [
        {
          text: '全部',
          filter: {},
        },
        {
          text: '基础资源库',
          filter: {
            子库: '基础技术领域资源库',
          },
        },
        {
          text: '行业资源库',
          filter: {
            子库: '行业领域资源库',
          },
        },
        {
          text: '专项解决方案',
          filter: {
            子库: '专项解决方案资源库',
          },
        },
      ],
      currentCategory: '',
    };
  },
  created() {
    if (this.Bus && this.Bus.$store && this.Bus.$store.state
      && this.Bus.$store.state.portal && this.Bus.$store.state.portal.filters
      && this.Bus.$store.state.portal.filters.resource) {
      this.currentCategory = this.Bus.$store.state.portal.filters.resource.filter || {};

      this.refreshData({ ...this.currentCategory });
    } else {
      this.refreshData();
    }
  },
  methods: {
    afterRefresh() {
      this.Bus.$store.commit('portal/CLEAR_FILTERS');

      // 获取领域字典列表
      const currentC = this.currentCategory
        && this.currentCategory.子库;
      this.getRequest(`portal/dataset/dict/fields?c=${currentC || ''}`).then((res) => {
        const opts = (res.data && res.data.fields) || [];

        if (opts && opts.length) {
          this.fieldsFilter = {
            label: '领域',
            field: '领域',
            multiple: false,
            options: (res.data && res.data.fields) || [],
          };
        }
      });
    },
    categoryChanged(f) {
      this.currentCategory = f;

      this.refreshData(f);

      this.filters = {};
      this.$refs.filterOptions.clearFilters();
    },
    pageChanged(p) {
      let filter = {};

      if (this.currentCategory) {
        filter = { ...this.currentCategory };
      }

      this.refreshData({ ...this.filters, ...filter, page: p });
    },
    filter(filters) {
      const { query } = filters;

      this.filters = query || {};
      let filter = {};

      if (this.currentCategory) {
        filter = { ...this.currentCategory };
      }

      this.refreshData({ ...this.filters, ...filter });
    },
  },
};
</script>

<style lang="scss" scoped>
.filter-options {
  font-size: 14px;
}
</style>
